﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Sensor.RadarLocalService;
using System.IO;

namespace Sensor {
    class Sensor {
        private readonly Timer t;
        private readonly int _maxSpeed;

        public Sensor(int maxSpeed) {
            t = new Timer(DoWork, null, 4000, 5000);
            _maxSpeed = maxSpeed;
        }

        private void DoWork(Object o) {
            RadarLocal radar = new RadarLocal();
            RadarLocalService.Capture c =
                new RadarLocalService.Capture { date = DateTime.Now, 
                    speed = (float)(new Random().NextDouble()*100),
                    plate = Guid.NewGuid().ToString(), 
                    type = (DateTime.Now.Second % 2 == 0 ? "Truck" : "Car")
                };

            List<RadarLocalService.Image> capImages = new List<RadarLocalService.Image>();
            for (int i = 0; i < 2; i++) {
                int fileCount = Directory.GetFiles(@"C:\Users\Ricardo Neto\Pictures\Wallpapers").Length;
                String filename = Directory.GetFiles(@"C:\Users\Ricardo Neto\Pictures\Wallpapers")[new Random().Next(0, fileCount-1)];
                FileStream s = new FileStream(filename, FileMode.Open, FileAccess.Read);
                byte[] image = new byte[s.Length];
                s.Read(image, 0, (int)s.Length);
                capImages.Add(new RadarLocalService.Image { image = image });
            }
            radar.StoreCapture(c, capImages.ToArray());
            Console.WriteLine("[SENSOR] Capture added!");
        }
    
    }
}
